
var MbPublisher = function() {
		
	this.publishAsJavaScript = function() {
	    if (!this.javaScriptPubDialog) {
	      this.javaScriptPubDialog = new ScriptPublishDialog("Publish as JavaScript", 'You can copy the script below and paste it to any web pages you like. Also you can modify it as you want based on this template.');
	    }
	    this.javaScriptPubDialog.setScriptText(this.publisher.generateJavaScript(null, null, this));
	    this.javaScriptPubDialog.show();
	}

	this.publishAsHTML = function() {
	    if (!this.htmlPubDialog) {
	      this.htmlPubDialog = new ScriptPublishDialog("Publish as HTML", 'You can copy the script below and paste it to any web pages you like. Also you can modify it as you want based on this template.');
	    }
	    this.htmlPubDialog.setScriptText(this.publisher.generateHTML(this));
	    this.htmlPubDialog.show();
	}
  
	this.publishGoogleGadget = function() {
	    if (!this.googleGadgetPubDialog) {
	      this.googleGadgetPubDialog = new ScriptPublishDialog("Publish as Google Gadget", 'You can copy the script below and paste it in a XML file and you can use as a Google gadget');
	    }
	    var buf = [];
	    buf.push('<?xml version="1.0" encoding="UTF-8" ?>');
	    buf.push('<Module>');
	    buf.push('<ModulePrefs title="" /> ');
	    buf.push('  <Content type="html">');
	    buf.push('    <![CDATA[');
	    buf.push(this.publisher.generateJavaScript(null, null, this));
	    buf.push('<div id="mydiv" />');
	    buf.push('  ]]>');
	    buf.push('</Content>');
	    buf.push('</Module>');
	    this.googleGadgetPubDialog.setScriptText(buf.join('\n'));
	    this.googleGadgetPubDialog.show();
	}
	
	this.publishNetvibesGadget = function() {
	    if (!this.netvibesGadgetPubDialog) {
	      this.netvibesGadgetPubDialog = new ScriptPublishDialog("Publish as Netvibes Gadget", 'You can copy the script below and paste it in a XML file and you can use as a Netvibes gadget');
	    }
	    var procConfig = this.rootProcessPanel.processDef.toConfig();
	    var buf = [];
	    buf.push('<?xml version="1.0" encoding="utf-8"?>');
	    buf.push('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
	    buf.push('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	    buf.push('<html xmlns="http://www.w3.org/1999/xhtml"');
	    buf.push('xmlns:widget="http://www.netvibes.com/ns/"  >');
	    buf.push('<head>');
	    buf.push('<meta name="author" content="" />');
	    buf.push('<meta name="description" content="'+procConfig.description+'" />');
	    buf.push('<meta name="apiVersion" content="1.0" />');
	    buf.push('<meta name="inline" content="true" />');
	    buf.push('<meta name="debugMode" content="false" />');
	    buf.push('<title>'+procConfig.name+'</title>');
	    buf.push('<link rel="icon" type="image/png" href="http://www.netvibes.com/favicon.ico" />');
	     buf.push('<link rel="stylesheet" type="text/css" href="http://www.netvibes.com/themes/uwa/style.css" />');
	    buf.push('<script type="text/javascript" src="http://www.netvibes.com/js/UWA/load.js.php?env=Standalone"></script>');
	    buf.push(this.publisher.generateJavaScript(false,true, this));
	    if(procConfig.params.length > 0) {
	      buf.push('<widget:preferences>');
	      jQuery.each(procConfig.params, function(i, param) {
	        buf.push('  <preference name="'+param.name+'" type="text" label="'+param.name+'" defaultValue="'+param['default']+'" />');
	      });
	      buf.push('</widget:preferences>');
	    }
	    buf.push('</head>');
	    buf.push('<body>');
	    buf.push('<div id="mydiv" />');
	    buf.push('</body>');
	    buf.push('</html>');
	    this.netvibesGadgetPubDialog.setScriptText(buf.join('\n'));
	    this.netvibesGadgetPubDialog.show();
	};
	
	/*
	 * Builds the JavaScript necessary to run a widget.
	 * Used by both the Publisher and the Test actions.
	 */
	this.generateJavaScript = function(onload, widget, mb_app) {
	    var procConfig = mb_app.rootProcessPanel.processDef.toConfig();
	    var buf = [];
		buf.push('<link rel="stylesheet" href="' + mb.packages.scriptBaseURL + '/renderers/style.css" type="text/css" />');
	    
	    jQuery.each(mb.lang.values(procConfig.requires), function(i, scriptURL) {
	      if(scriptURL.endsWith("js/mb/renderers/smile-widgets.js")){
		  	buf.push('<script src="http://api.simile-widgets.org/timeplot/1.1/timeplot-api.js" type="text/javascript"></script>');
	      }
	    });
		buildCoreScriptIncludes(buf);
				
	    jQuery.each(mb.lang.values(procConfig.requires), function(i, scriptURL) {
	      buf.push('<script type="text/javascript" src="'+scriptURL+'"></script>');
	    });
	
	    delete procConfig.requires;
	
	    buf.push('<script type="text/javascript">');
	    if (onload) {
			buf.push('function f(){');
		}
	    buf.push('  var procdef = new mb.ProcessDef('+mb.lang.toJSON(procConfig)+');');
	    buf.push('  var proc = new mb.ProcessInstance(procdef);');
	    jQuery.each(procConfig.params, function(i, param) {
	      if(widget)
	        buf.push("  var paramValue"+i+" = widget.getValue('"+param.name+"');");
	      else
	        buf.push("  var paramValue"+i+" = mb.url.getUrlParameters()['"+param.name+"'];");
	      buf.push("  if(!paramValue"+i+")");
	      buf.push("    paramValue"+i+" = '"+param['default']+"';");
	      buf.push("  proc.setParam('"+param.name+"', paramValue"+i+");");
	    })
	    buf.push('');
	    buf.push('  proc.start(function(result) {');
	    buf.push('    result.render(document.getElementById("mydiv"));');
	    buf.push('  })');
	    if(onload)
	      buf.push('}');
	    buf.push('</script>');
	    return buf.join('\n');
	}

	this.generateHTML = function(mb_app) {
		var buf = [];
		buf.push('<html>');
		buf.push('<head>');
		buf.push('<title>Mashup Builder - HTML Example</title>');
		buf.push(this.generateJavaScript(true, null, mb_app));
		buf.push('</head>');
		buf.push('<body onload="f()">');
		buf.push('<div id="mydiv"></div>');
		buf.push('</body>');
		buf.push('</html>');
		return buf.join('\n');
	}
	
	function buildCoreScriptIncludes(buf) {
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/../jquery/jquery-1.4.2.min.js"></script>')
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-config.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-lang.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-dom.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-async.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-core.js"></script>');
	    buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-el.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-ajax.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-process.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-action.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/runtime/mb-render.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/operators/array-operators.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/operators/basic-operators.js"></script>');
		buf.push('<script type="text/javascript" src="'+mb.packages.scriptBaseURL+'/operators/string-operators.js"></script>');
	}
};
